//
//  Tool.m
//  
//
//  Created by qkong on 15/6/13.
//
//

#import "Tool.h"

@interface Tool ()



@end

@implementation Tool

+ (void)peripheralsDidUpdates:(NSMutableArray *)peripherals{
    NSNotification *notification = [NSNotification notificationWithName:PERIPHERALS_UPDATE_NOTIFICATIOTN_NAME
                                                                 object:nil
                                                               userInfo:@{kKeyNotificationUserInfoPeripherals:peripherals}];
    [[NSNotificationCenter defaultCenter]postNotification:notification];
}

+ (CBUUID *)getDataCharacteristicUUID{
    return [CBUUID UUIDWithString:GET_DATA_CHARACTERISTIC_UUID];
}

+ (NSNumber *)getData:(NSData *)data atStart:(NSUInteger)start{
    const char* bytes  = (const char*)[data bytes];
    int l_data = bytes[start];
    int h_data = bytes[start+1];
    if (l_data == -1 && h_data == -1) {
        return nil;
    }
    return [NSNumber numberWithInt:l_data+h_data*16*16];
}

+ (NSData *)dataWithHexString:(NSString *)hex
{
    char buf[3];
    buf[2] = '\0';
    NSAssert(0 == [hex length] % 2, @"Hex strings should have an even number of digits (%@)", hex);
    unsigned char *bytes = malloc([hex length]/2);
    unsigned char *bp = bytes;
    for (CFIndex i = 0; i < [hex length]; i += 2) {
        buf[0] = [hex characterAtIndex:i];
        buf[1] = [hex characterAtIndex:i+1];
        char *b2 = NULL;
        *bp++ = strtol(buf, &b2, 16);
        NSAssert(b2 == buf + 2, @"String should be all hex digits: %@ (bad digit around %d)", hex, i);
    }
    
    return [NSData dataWithBytesNoCopy:bytes length:[hex length]/2 freeWhenDone:YES];
}

+ (NSString *)dataToHexString:(NSData *)data
{
    const unsigned char *dbytes = [data bytes];
    NSMutableString *hexStr =
    [NSMutableString stringWithCapacity:[data length]*2];
    int i;
    for (i = 0; i < [data length]; i++) {
        [hexStr appendFormat:@"%02x", dbytes[i]];
    }
    return [NSString stringWithString: hexStr];
}

+ (NSString *)dateNowHourAndMinuteFormat{
    NSDate *now = [NSDate new];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"mm:ss";
    NSString *hrDateString = [dateFormatter stringFromDate:now];
    return hrDateString;
}


@end
